﻿using Base.Repository.Interface;
using Base.Service.EFCore.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Base.Service.EFCore.MySql
{
    public class BaseService<T> : IBaseService<T> where T : class, new()
    {
        private readonly IRepository<T> _repository;

        public BaseService(IRepository<T> repository)
        {
            _repository = repository;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="t"></param>
        public void Add(T t)
        {
            _repository.Add(t);
        }

        /// <summary>
        /// 多个新增
        /// </summary>
        /// <param name="ie"></param>
        public void AddRange(IEnumerable<T> ie)
        {
            _repository.AddRange(ie);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="t"></param>
        public void Delete(T t)
        {
            _repository.Delete(t);
        }

        /// <summary>
        /// 多个删除
        /// </summary>
        /// <param name="ie"></param>
        public void DeleteRange(IEnumerable<T> ie)
        {
            _repository.DeleteRange(ie);
        }

        /// <summary>
        /// 还是查询所有
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public IQueryable<T> QueryAll(Expression<Func<T, bool>>? where)
        {
            return _repository.QueryAll(where);
        }

        /// <summary>
        /// 查询排序
        /// </summary>
        /// <typeparam name="type"></typeparam>
        /// <param name="order"></param>
        /// <param name="isAsc"></param>
        /// <param name="where"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public IQueryable<T> QueryAll<type>(Expression<Func<T, type>> order, bool isAsc = true, Expression<Func<T, bool>>? where = null)
        {
            return _repository.QueryAll(order, isAsc, where);
        }

        /// <summary>
        /// 查询分页
        /// </summary>
        /// <typeparam name="type"></typeparam>
        /// <param name="total"></param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="order"></param>
        /// <param name="isAsc"></param>
        /// <param name="where"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public IQueryable<T> QueryAll<type>(out int total, int page, int limit, Expression<Func<T, type>> order, bool isAsc = true, Expression<Func<T, bool>>? where = null)
        {
            return _repository.QueryAll(out total, (page - 1) * limit, limit, order, isAsc, where);
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public int SaveChanges()
        {
            return _repository.SaveChanges();
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="t"></param>
        /// <exception cref="NotImplementedException"></exception>
        public void Update(T t)
        {
           _repository.Update(t);
        }

        /// <summary>
        /// 多个修改
        /// </summary>
        /// <param name="ie"></param>
        /// <exception cref="NotImplementedException"></exception>
        public void UpdateRange(IEnumerable<T> ie)
        {
             _repository.UpdateRange(ie);
        }
    }
}
